package com.company.word.dao;

import com.company.word.pojo.Word;
import com.company.word.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class WordDAOImpl implements IWordDAO{


	@Override
	public int insert(Word w) throws SQLException {
		String sql="insert into word values(null,?,?,?)";
		Connection conn=JDBCUtils.getConnection();
		PreparedStatement stmt=conn.prepareStatement(sql);
		stmt.setString(1,w.getWorld());
		stmt.setString(2,w.getChinese());
		stmt.setString(3,w.getUserId());
		int result;
		try{
			result=stmt.executeUpdate();}
		catch (SQLException throwables) {
			throwables.printStackTrace();
			result=-1;
		}
		System.out.println("影响行数"+result);
		JDBCUtils.destory(conn,stmt,null);
		return result;
	}



	@Override
	public int deleteByID(int id) throws SQLException {
		System.out.println(id);
		String sql="delete from word  where id =? ";
		Connection conn=JDBCUtils.getConnection();

		PreparedStatement stmt=conn.prepareStatement(sql);
		stmt.setInt(1,id);

		int result=-1;
		try{
			result=stmt.executeUpdate();
	}
		catch (Exception e)
		{
			System.out.println(e);
			result=-1;
		}
		System.out.println("影响行数"+result);

		JDBCUtils.destory(conn,stmt,null);
		return result;
	}



	@Override
	public int update(Word w) throws SQLException {
		String sql = "update word set  word=?, chinese=? ,user_id=? where id = ?";
		System.out.println(w.getId()+"userid");


		// 1. 得到连接
		Connection conn = JDBCUtils.getConnection();
		// 2. 创建查询对象
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setString(1, w.getWorld());
		stmt.setString(2, w.getChinese());
		stmt.setString(3, w.getUserId());
		stmt.setInt(4, w.getId());
		// 3. 执行
		int result=-1;
		try {
			result= stmt.executeUpdate();
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		System.out.println("影响行数:"+result);
		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);
		return result;
	}

	@Override
	public Word findByWord(String word) throws SQLException {
		String sql = "select * from word where word = ?";
		// 1. 得到连接
		Connection conn = JDBCUtils.getConnection();
		// 2. 创建查询对象
		PreparedStatement stmt = conn.prepareStatement(sql);
		stmt.setString(1, word);
		// 3. 执行
		ResultSet rs = stmt.executeQuery();
		Word w = null;
		if(rs.next())
		{
			int id=rs.getInt(1);
			String Word = rs.getString(2);
			String English = rs.getString(3);
			String Chinese = rs.getString(4);
			w = new Word(id,Word,English,Chinese);
		}

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return w;
	}

	@Override
	public List<Word> findAll() throws SQLException {

		String sql = "select * from word";
		// 1. 得到连接
		Connection conn = JDBCUtils.getConnection();
		// 2. 创建查询对象
		PreparedStatement stmt = conn.prepareStatement(sql);
		// 3. 执行
		ResultSet rs = stmt.executeQuery();
		List <Word> list=new ArrayList<>();
		while(rs.next())
		{
			int id = rs.getInt(1);
			String Word = rs.getString(2);
			String chinese = rs.getString(3);
			String userId = rs.getString(4);
			Word s = new Word(id,Word,chinese,userId);
			list.add(s);
		}

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return null;
	}
}
